package com.liao.seckill.rabbitmq;

import com.alibaba.fastjson.JSON;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class MiaoShaSender {

    private final AmqpTemplate amqpTemplate;
    private final AmqpAdmin amqpAdmin;

    public MiaoShaSender(AmqpTemplate amqpTemplate, AmqpAdmin amqpAdmin) {
        this.amqpTemplate = amqpTemplate;
        this.amqpAdmin = amqpAdmin;
    }

    @Async
    public void sendMiaoShaMessage(MiaoShaMessage miaoShaMessage) {
        String msg = JSON.toJSONString(miaoShaMessage);
        amqpTemplate.convertAndSend(RabbitMQConfig.QUEUE_NAME, msg);
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
